package frog.lazy.framework.keeper.filter;


import cn.hutool.core.collection.ListUtil;
import frog.lazy.framework.keeper.provider.IAccountProvider;
import frog.lazy.framework.keeper.provider.IPermProvider;
import frog.lazy.framework.keeper.utils.JWTUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.Filter;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 */
@Slf4j
@Component
public class ShiroFilterChainManager {

    @Value("${shiro.login:/login/**,/anon/**}")
    private String[] anons;

    @Value("${server.servlet.context-path}")
    private String contextPath;

    @Resource
    private JWTUtils jwtUtils;
    @Autowired
    private IAccountProvider accountProvider;

    @Autowired
    private IPermProvider permProvider;


    public Map<String, Filter> initGetFilters() {
        Map<String, Filter> filters = new LinkedHashMap<>();
        PasswordFilter passwordFilter = new PasswordFilter(jwtUtils, accountProvider);
        filters.put("login",passwordFilter);
        JWTAuthFilter jwtFilter = new JWTAuthFilter(jwtUtils, permProvider);
        filters.put("jwt",jwtFilter);
        return filters;
    }

    public Map<String,String> initGetFilterChain() {
        Map<String,String> filterChain = new LinkedHashMap<>();

        filterChain.put("/swagger-ui.html", "anon");
        filterChain.put("/webjars/**", "anon");
        filterChain.put("/v2/**", "anon");
        filterChain.put("/swagger-resources/**", "anon");
        filterChain.put("/doc.html/**", "anon");

        List<String> defalutAnon = ListUtil.toList("/css/**","/js/**", "/anon/**");
        defalutAnon.forEach(ignored -> filterChain.put(ignored,"anon"));

        List<String> defalutAuth = ListUtil.toList("/biz/**");
        defalutAuth.forEach(auth -> filterChain.put(auth,"jwt"));

        List<String> defalutLogin = ListUtil.toList("/**");
        defalutLogin.forEach(login -> filterChain.put(login, "login"));

        return filterChain;
    }

}
